package com.car.account.controller;


import cn.hutool.core.bean.BeanUtil;
import com.car.account.controller.form.userCarForm.UserBindCarForm;
import com.car.account.dto.userCarDto.UserCarDto;
import com.car.account.param.userCarParam.UserBindCarParam;
import com.car.account.service.UserCarService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/userCar")
@Api(tags = "用户车辆信息")
public class UserCarController {

    @Resource
    private UserCarService userCarService;

    @GetMapping("/selectCarByUserId")
    @ApiOperation(value = "通过用户id查找车辆")
    public Result<List<UserCarDto>> selectCarByUserId() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        List<UserCarDto> userCarDtos = userCarService.selectCarByUserId(userId);
        return Result.getListResult(UserCarDto.class).setData(userCarDtos).setMsg("通过用户id查找车辆成功");
    }

    @PostMapping("/userBindCar")
    @ApiOperation(value = "用户绑定（新增）车辆")
    public Result userBindCar(@RequestBody UserBindCarForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        UserBindCarParam param = BeanUtil.toBean(form, UserBindCarParam.class);
        param.setUserId(userId);
        userCarService.userBindCar(param);
        return Result.getResult().setMsg("用户绑定车辆成功");
    }

    @GetMapping("/userUnbindingCar")
    @ApiOperation(value = "用户解绑（删除）车辆")
    public Result userUnbindingCar(Integer userCarId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer userId = JwtUtil.getUserId(token);
        userCarService.userUnbindingCar(userId, userCarId);
        return Result.getResult().setMsg("用户解绑车辆成功");
    }

}

